package com.irun.cm;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/***
 * @author huangsm
 * @date 2012-4-9
 * @email huangsanm@gmail.com
 * @desc 程序启动
 */
public class CallsManagerActivity extends Activity {

	// private final static String TAG = "CallManager";
	public static final String TITLE = "title";

	private int[] mThumbs = { R.drawable.call, R.drawable.message,
			R.drawable.email, R.drawable.search, R.drawable.intercept,
			R.drawable.call_settings, R.drawable.communication, R.drawable.ip };

	private String[] intents;
	private Context mContext;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		/*
		 * requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		 * getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
		 * R.layout.titlebar);
		 */
		setTitle(R.string.app_name);
		setContentView(R.layout.main);

		intents = getResources().getStringArray(R.array.start_intents);
		mContext = this;

		GridView gridView = (GridView) findViewById(R.id.main_list);
		gridView.setAdapter(new ItemAdapter(mContext));
		gridView.setOnItemClickListener(new GridView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (TextUtils.isEmpty(intents[position])) {
					return;
				}
				Intent intent = new Intent();
				intent.setClassName(getPackageName(), intents[position]);
				startActivity(intent);
			}
		});
	}

	/***
	 * 数据适配器
	 * 
	 * @author Administrator
	 */
	public class ItemAdapter extends BaseAdapter {
		private String[] mItems;
		private LayoutInflater mInflater;

		public ItemAdapter(Context context) {
			mItems = getResources().getStringArray(R.array.apps_main);
			mInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			return mThumbs.length;
		}

		@Override
		public Object getItem(int position) {
			return mThumbs[position];
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			HolderView view;
			if (convertView == null) {
				convertView = (LinearLayout) mInflater.inflate(
						R.layout.grid_items, null);
				view = new HolderView();
				view.title = (TextView) convertView.findViewById(R.id.title);
				view.icon = (ImageView) convertView.findViewById(R.id.icon);
				convertView.setTag(view);
			} else {
				view = (HolderView) convertView.getTag();
			}
			view.title.setText(mItems[position]);
			view.icon.setImageResource(mThumbs[position]);
			return convertView;
		}

		public class HolderView {

			private TextView title;
			private ImageView icon;
		}
	}
}